package com.smart.community.property.controller;

import com.smart.community.commons.result.Result;
import com.smart.community.property.entity.RepairProgress;
import com.smart.community.property.service.IRepairProgressService;
import com.smart.community.commons.annotation.ApiLog;
import com.smart.community.commons.enums.ApiLogOperationType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 维修工单进度记录控制器
 * 
 * 严格按照《Java后端SpringBoot代码开发规范.md》Entity规范实现：
 * - 功能单一原则：Controller层负责接口暴露，Entity层只负责数据库字段映射
 * - 类型一致性：确保与Entity字段类型完全一致，progressType和status使用String类型
 * - 生产环境标准：移除throws Exception声明，使用统一异常处理
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 * @version 2.0.0 - 根据新Entity规范调整
 */
@Slf4j
@RestController
@RequestMapping("/property/repair-progress")
@Tag(name = "维修工单进度管理", description = "维修工单进度记录的查询接口")
public class RepairProgressController {

    @Autowired
    private IRepairProgressService repairProgressService;

    @GetMapping("/list")
    @Operation(summary = "根据工单ID查询进度记录列表", description = "查询指定工单的所有进度记录")
    public Result<List<RepairProgress>> getProgressListByOrderId(
            @Parameter(description = "工单ID") @RequestParam Long orderId) {
        log.info("查询工单进度记录，工单ID: {}", orderId);
        List<RepairProgress> progressList = repairProgressService.getProgressListByOrderId(orderId);
        return Result.success(progressList);
    }

    @GetMapping("/list-by-order-no")
    @Operation(summary = "根据工单号查询进度记录列表", description = "查询指定工单号的所有进度记录")
    public Result<List<RepairProgress>> getProgressListByOrderNo(
            @Parameter(description = "工单号") @RequestParam String orderNo) {
        log.info("查询工单进度记录，工单号: {}", orderNo);
        List<RepairProgress> progressList = repairProgressService.getProgressListByOrderNo(orderNo);
        return Result.success(progressList);
    }

    @GetMapping("/latest")
    @Operation(summary = "获取工单最新进度记录", description = "获取指定工单的最新进度记录")
    public Result<RepairProgress> getLatestProgressByOrderId(
            @Parameter(description = "工单ID") @RequestParam Long orderId) {
        log.info("查询工单最新进度记录，工单ID: {}", orderId);
        RepairProgress latestProgress = repairProgressService.getLatestProgressByOrderId(orderId);
        return Result.success(latestProgress);
    }

    @GetMapping("/count")
    @Operation(summary = "统计工单进度数量", description = "统计指定工单的进度记录数量")
    public Result<Integer> countProgressByOrderId(
            @Parameter(description = "工单ID") @RequestParam Long orderId) {
        log.info("统计工单进度数量，工单ID: {}", orderId);
        int count = repairProgressService.countProgressByOrderId(orderId);
        return Result.success(count);
    }

    @GetMapping("/by-type")
    @Operation(summary = "根据进度类型查询进度记录", description = "查询指定工单和进度类型的进度记录")
    public Result<RepairProgress> getProgressByOrderIdAndType(
            @Parameter(description = "工单ID") @RequestParam Long orderId,
            @Parameter(description = "进度类型：1-创建，2-受理，3-开始维修，4-完成，5-取消")
            @RequestParam Integer progressType) {
        log.info("查询工单指定类型进度记录，工单ID: {}, 进度类型: {}", orderId, progressType);
        RepairProgress progress = repairProgressService.getProgressByOrderIdAndType(orderId, progressType);
        return Result.success(progress);
    }

    @GetMapping("/by-time-range")
    @Operation(summary = "查询时间范围内的进度记录", description = "查询指定时间范围内的所有进度记录")
    public Result<List<RepairProgress>> getProgressListByTimeRange(
            @Parameter(description = "开始时间") @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        log.info("查询时间范围内进度记录，开始时间: {}, 结束时间: {}", startTime, endTime);
        List<RepairProgress> progressList = repairProgressService.getProgressListByTimeRange(startTime, endTime);
        return Result.success(progressList);
    }

    @GetMapping("/by-repairman")
    @Operation(summary = "查询维修人员相关的进度记录", description = "查询指定维修人员在时间范围内的进度记录")
    public Result<List<RepairProgress>> getProgressListByRepairmanId(
            @Parameter(description = "维修人员ID") @RequestParam Long repairmanId,
            @Parameter(description = "开始时间") @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        log.info("查询维修人员进度记录，维修人员ID: {}, 开始时间: {}, 结束时间: {}", repairmanId, startTime, endTime);
        List<RepairProgress> progressList = repairProgressService.getProgressListByRepairmanId(repairmanId, startTime, endTime);
        return Result.success(progressList);
    }

    @PostMapping("/create")
    @Operation(summary = "手动创建进度记录", description = "手动创建维修工单进度记录")
    @ApiLog(
        logTitle = "创建维修进度记录",
        logType = 2,
        moduleName = "物业管理",
        operationType = ApiLogOperationType.ADD
    )
    public Result<Boolean> createProgress(@RequestBody RepairProgress progress) {
        log.info("手动创建进度记录，工单ID: {}, 进度类型: {}", progress.getOrderId(), progress.getProgressType());
        boolean success = repairProgressService.saveProgress(progress);
        return Result.success(success);
    }

    @PostMapping("/create-simple")
    @Operation(summary = "简单创建进度记录", description = "使用简化参数创建维修工单进度记录")
    @ApiLog(
        logTitle = "简单创建维修进度记录",
        logType = 2,
        moduleName = "物业管理",
        operationType = ApiLogOperationType.ADD
    )
    public Result<Boolean> createSimpleProgress(
            @Parameter(description = "工单ID") @RequestParam Long orderId,
            @Parameter(description = "工单号") @RequestParam String orderNo,
            @Parameter(description = "进度类型") @RequestParam Integer progressType,
            @Parameter(description = "进度标题") @RequestParam String progressTitle,
            @Parameter(description = "进度内容") @RequestParam String progressContent,
            @Parameter(description = "操作人ID") @RequestParam(required = false) Long operatorId,
            @Parameter(description = "操作人姓名") @RequestParam(required = false) String operatorName) throws Exception {
        log.info("简单创建进度记录，工单ID: {}, 进度类型: {}", orderId, progressType);
        boolean success = repairProgressService.createProgress(
            orderId, orderNo, progressType, progressTitle, progressContent, operatorId, operatorName);
        return Result.success(success);
    }
} 